package com.xcity.game.combat.calc.impl;

import java.util.List;

import com.xcity.db.entity.Employee;
import com.xcity.game.combat.attr.CombatAttribute;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.calc.CombatAttributeCalculator;
import com.xcity.game.employee.EmployeeService;
import com.xcity.game.employee.config.impl.EmployeeBlessConfigure;
import com.xcity.game.employee.template.EmployeeBlessTemplate;
import com.xcity.game.employee.template.EmployeeQualityUpTemplate;
import com.xcity.game.employee.template.EmployeeStarUpTemplate;
import com.xcity.game.equip.EquipUtils;
import com.xcity.game.skill.v2.DefaultSkillV2;
import com.xcity.pomelo.response.EquipmentBagResponse.EquipmentSuit;
import com.xcity.pomelo.response.EquipmentBagResponse.EquipmentSuitEffect;

import naga.x.App;

public class EmployeeCombatAttributeCalculator extends CombatAttributeCalculator<Employee> {

	public EmployeeCombatAttributeCalculator(Employee unit) {
		super(unit);
	}

	@Override
	protected float calc(CombatAttributeType type) {
		// 总数值=基础值+技能加成+羁绊加成+装备加成
		// 1. 基础值=升级+升品+升星/升天命
			// 升级
		float val = unit.getAttributeTemplate().calc(type, unit.getLevel());
			// 升品
		if (unit.getQualityLvl() > 0) {
			List<EmployeeQualityUpTemplate> templates = App.getApp().getServiceManager().get(EmployeeService.class).configure().getQualityLevelUpConfigure().getTemplates(unit.getTemplate().getUpQualityId());
			for (int i = 0, size = templates.size(); i < size; i++) {
				EmployeeQualityUpTemplate template = templates.get(i);
				if (template.getLevel() > unit.getQualityLvl()) {
					break;
				}
				val += template.getAttributeValue(type);
			}
		}
			// 升星
		if (unit.getStarLvl() > 0) {
			List<EmployeeStarUpTemplate> templates = App.getApp().getServiceManager().get(EmployeeService.class).configure().getStarLevelUpConfigure().getTemplates(unit.getTemplate().getId());
			for (int i = 0, size = templates.size(); i < size; i++) {
				EmployeeStarUpTemplate template = templates.get(i);
				if (template.getLevel() > unit.getStarLvl()) {
					break;
				}
				val += template.getAttributeValue(type);
			}
		}
		float baseVal = val;
		// 2. 技能加成
		List<DefaultSkillV2> skills = unit.getSkillManager().getPassives();
		for (int i = 0, size = skills.size(); i < size; i++) {
			val += skills.get(i).enhance(type, baseVal);
		}
		// 3. 羁绊加成
		List<CombatAttribute> combos = unit.getCombos();
		for (int i = 0, size = combos.size(); i < size; i++) {
			CombatAttribute combo = combos.get(i);
			if (combo.getType() == type) {
				val += combo.doFilter(baseVal, combo.getValue());
			}
		}
		// 4. 装备加成
		val += EquipUtils.calcCombatAttributeValue(unit.getEquipments(), type);
		// 5. 套装效果
		List<EquipmentSuit> suits = unit.getEquipSuits();
		for (int i = 0; i < suits.size(); i++) {
			List<EquipmentSuitEffect> effects = suits.get(i).effects;
			for (int j = 0; j < effects.size(); j++) {
				CombatAttribute enhanced = effects.get(j).getEnhancedAttribute(type);
				if (enhanced != null) {
					val += enhanced.doFilter(baseVal, enhanced.getValue());
				}
			}
		}
		// 6. 开光
		int[] blessLvls = unit.getBlessLvls();
		if (blessLvls != null && blessLvls.length > 0) {
			EmployeeBlessConfigure config = App.getApp().getServiceManager().get(EmployeeService.class).configure().getBlessConfigure();
			int[] blessIds = unit.getTemplate().getBlessId();
			for (int slot = 0; slot < blessLvls.length; slot++) {
				int blessId = blessIds[slot];
//						for(int i = 0, size = unit.getBlessLvls()[slot]; i < size; i++){
//							val += lvl2temp.get(i).getAttributeValue(type);
//						}
				int lvl = blessLvls[slot];
				if (lvl > 0) {
					List<EmployeeBlessTemplate> lvl2temp = config.getBlessId2TempList().get(blessId);
					val += lvl2temp.get(lvl - 1).getTotalAttsValue(type);
				}
			}
		}
		return val;
	}

}
